extends Control

var dontquit=false
var can_show_panel=true
var can_press=true

func _process(_delta):
	if sys.cur_sce==sys.scene.gaming&&i.keys.cancel&&can_show_panel:
		if !get_tree().paused:
			sys.pointer.show()
			$"/root/sys/gaming".tmr.paused=true
			get_tree().paused=true
			sys.smcr.movable=false
#			sys.cm.color.r/=2
#			sys.cm.color.g/=2
#			sys.cm.color.b/=2
			$"sms".stop_all()
			$"sms".remove_all()
			$"sms".interpolate_property(self,"rect_position",Vector2(0,-512),Vector2(0,0),1,Tween.TRANS_CUBIC,Tween.EASE_OUT)
			$"sms".start()
		elif !dontquit:
			$"/root/sys/gaming".tmr.paused=false
			get_tree().paused=false
			sys.smcr.movable=true
#			sys.cm.color.r*=2
#			sys.cm.color.g*=2
#			sys.cm.color.b*=2
			$"sms".stop_all()
			$"sms".remove_all()
			$"sms".interpolate_property(self,"rect_position",Vector2(0,0),Vector2(0,-512),1,Tween.TRANS_CUBIC,Tween.EASE_OUT)
			sys.pointer_mover.stop_all()
			sys.pointer_mover.remove_all()
			$"sms".interpolate_property(sys.pointer,"rect_position",sys.pointer.rect_position,Vector2(0,-64),1,Tween.TRANS_CUBIC,Tween.EASE_OUT)
			$"sms".start()

func move_pointer(node):
	if(!sys.changing_scene):
		sys.pointer_mover.interpolate_property(sys.pointer,"rect_position",sys.pointer.rect_position,Vector2(node.rect_position.x+16,node.rect_position.y),0.2,Tween.TRANS_CUBIC,Tween.EASE_OUT)
		sys.pointer_mover.interpolate_property(node,"rect_position",node.rect_position,Vector2(node.rect_position.x,node.rect_position.y+20),0.2,Tween.TRANS_CUBIC,Tween.EASE_OUT)
		sys.pointer_mover.start()

func reset_button(node,pos):
	if(!sys.changing_scene):
		sys.pointer_mover.interpolate_property(sys.pointer,"rect_position",sys.pointer.rect_position,Vector2(pos.x+16,pos.y-60),0.2,Tween.TRANS_CUBIC,Tween.EASE_OUT)
		sys.pointer_mover.interpolate_property(node,"rect_position",node.rect_position,pos,0.2,Tween.TRANS_CUBIC,Tween.EASE_OUT)
		sys.pointer_mover.start()

func save_mouse_enter():
	move_pointer($"save")

func save_mouse_exit():
	reset_button($"save",Vector2(205,256))

func load_enter():
	move_pointer($"load")

func main_menu_enter():
	move_pointer($"main_menu")

func settings_enter():
	move_pointer($"settings")

func load_exit():
	reset_button($"load",Vector2(410,256))

func main_menu_exit():
	reset_button($"main_menu",Vector2(615,256))

func settings_exit():
	reset_button($"settings",Vector2(820,256))

func save_in(event):
	if(event is InputEventMouseButton):
		if(event.button_index==BUTTON_LEFT&&event.pressed&&!$"/root/sys/gaming/AnimationPlayer".is_playing()&&can_press):
			can_press=false
			yield(sys.alert("是否确定覆写存档为当前状态？注意此操作不可撤销！"),"completed")
			can_press=true
			if sys.if_user_agrees:
				sys.save()

func load_in(event):
	if(event is InputEventMouseButton):
		if(event.button_index==BUTTON_LEFT&&event.pressed&&can_press):
			can_press=false
			yield(sys.alert("是否确定从存档加载状态？注意未存档进度将会消失！"),"completed")
			can_press=true
			if sys.if_user_agrees:
				sys.gload()

func main_menu_in(event):
	if(event is InputEventMouseButton):
		if(event.button_index==BUTTON_LEFT&&event.pressed&&!$"/root/sys/gaming/AnimationPlayer".is_playing()&&can_press):
			can_press=false
			can_show_panel=false
			yield(sys.alert("是否确定回到主菜单？注意未存档进度将会消失！"),"completed")
			if !sys.if_user_agrees:
				can_press=true
				can_show_panel=true
				return
			get_tree().paused=false
			sys.smcr.movable=true
#			sys.cm.color.r*=2
#			sys.cm.color.g*=2
#			sys.cm.color.b*=2
			$"sms".stop_all()
			$"sms".remove_all()
			$"sms".interpolate_property(self,"rect_position",Vector2(0,0),Vector2(0,-512),1,Tween.TRANS_CUBIC,Tween.EASE_OUT)
			sys.pointer_mover.stop_all()
			sys.pointer_mover.remove_all()
			$"sms".interpolate_property(sys.pointer,"rect_position",sys.pointer.rect_position,Vector2(0,-64),1,Tween.TRANS_CUBIC,Tween.EASE_OUT)
			$"sms".start()
			yield($"sms","tween_all_completed")
			can_press=true
			sys.ret_mainmenu()

func settings_in(event):
	if(event is InputEventMouseButton):
		if(event.button_index==BUTTON_LEFT&&event.pressed&&can_press):
			can_press=false
			can_show_panel=false
			yield(sys.alert("是否确定打开设置？注意未存档进度将会消失！"),"completed")
			if !sys.if_user_agrees:
				can_press=true
				can_show_panel=true
				return
			get_tree().paused=false
			sys.smcr.movable=true
#			sys.cm.color.r*=2
#			sys.cm.color.g*=2
#			sys.cm.color.b*=2
			$"sms".stop_all()
			$"sms".remove_all()
			$"sms".interpolate_property(self,"rect_position",Vector2(0,0),Vector2(0,-512),1,Tween.TRANS_CUBIC,Tween.EASE_OUT)
			sys.pointer_mover.stop_all()
			sys.pointer_mover.remove_all()
			$"sms".interpolate_property(sys.pointer,"rect_position",sys.pointer.rect_position,Vector2(0,-64),1,Tween.TRANS_CUBIC,Tween.EASE_OUT)
			$"sms".start()
			yield($"sms","tween_all_completed")
			can_press=true
			sys.ret_settings()

func y_enter():
	move_pointer($"../../alert/y")

func n_enter():
	move_pointer($"../../alert/n")

func y_exit():
	reset_button($"../../alert/y",Vector2(350,340))

func n_exit():
	reset_button($"../../alert/n",Vector2(550,340))
